<?php
/**
 * @file name : consultantcategory_model.php
 * @package   : thokim
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Consultantcategory_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
    public function __construct()
    {
        parent::__construct();
        $this->_tbl        = 'consultant_category';
        $this->_primary_id = 'category_id';
    }
    /**
     * getTotalFields
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @param  string
     *
     * @return int
     */
    public function getTotalFields($field = '*')
    {
        return $this->db->count_field_results($this->_tbl, $field);
    }
    /**
     * @function loadListIn
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      public
     * @param       string
     * @param       array
     * @param     string
     * @param     string
     * @return     array
     */
    public function loadListIn($field, $arrWhereIn = array(), $order = '', $mode = '', $arrWhere = array())
    {
        if (! is_string($field) || strlen($field) <= 0) {
            return NULL;
        }
        if (count($arrWhereIn) <= 0) {
            return NULL;
        }

        $this->db->where_in($field, $arrWhereIn);
        if (count($arrWhere) > 0) {
            $this->db->where($arrWhere);
        }
        if (is_string($order) && strlen($order) > 0) {
            if ( ! preg_match('/(asc|ASC|desc|DESC)/', $mode)) {
                $mode = 'asc';
            }
            $this->db->order_by($order, $mode);
        }

        $query = $this->db->get($this->_tbl);
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
        return $arrList;
    }
    /**
     * @function loadRecord
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @param  int
     * @return array
     */
    public function loadRecord($pri_id)
    {

        $where = array($this->_primary_id  => $pri_id);

        $itemObj = $this->db->get_where($this->_tbl, $where);
        $arrList = array();
        if ($itemObj->num_rows() > 0) {
            foreach($itemObj->result() as $row) {
               $arrList[] = $row;
            }
        }
        return $arrList;
    }
    /**
     * recursiveLoadCategory
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     *
     *
     */
    public function recursiveLoadCategory($parent_id = NULL, $arrCategory, $selected = '', $res = '',
                                          $delimiter = '')
    {
        if ( ! is_array($arrCategory) || empty($arrCategory)) {
            return;
        }
        foreach($arrCategory as $key => $category) {
            if ($category->parent_id == $parent_id) {
                if ($category->category_id == $selected) {
                    $tmp = '<option value="' . $category->category_id. '" selected>'
                         . $delimiter . $category->category_name . '</option>' . "\n";
                } else {
                    $tmp = '<option value="' . $category->category_id. '">'
                         . $delimiter . $category->category_name . '</option>' . "\n";
                }
                $res .= $this->recursiveLoadCategory($category->category_id, $arrCategory, $selected, $tmp,
                                                     $delimiter . '&nbsp;&nbsp;-->');
            }
        }

        return $res;
    }
    /**
     * recursiveLoadChild
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @return array
     */
    public function recursiveLoadChild($parent_id = NULL, $arrCategory, $arrRes = array())
    {
        if ( ! is_array($arrCategory) || empty($arrCategory)) {
            return;
        }

        foreach($arrCategory as $category) {
            if ($category->parent_id == $parent_id) {
                array_push($arrRes, $category);
                $arrRes = $this->recursiveLoadChild($category->category_id, $arrCategory, $arrRes);
            }
        }

        return $arrRes;
    }
}
